package com.chenyun.system.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.chenyun.common.annotation.Log;
import com.chenyun.common.annotation.Page;
import com.chenyun.common.enums.operlog.BusinessTypeEnum;
import com.chenyun.system.model.SysDictType;
import com.chenyun.system.service.ISysDictTypeService;
import com.chenyun.boot.web.controller.BaseController;
import com.chenyun.common.constants.UserConstants;
import com.chenyun.common.vo.AjaxResult;
import com.chenyun.common.vo.PageResult;
import com.chenyun.system.vo.TreeSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/dict/type")
@Api(tags = "数据字典类型-admin")
public class SysDictTypeController extends BaseController<ISysDictTypeService, SysDictType> {
    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation("获取数据字典类型列表")
    @PostMapping("/list")
    @SaCheckPermission("system:dict:type:list")
    @Page
    public PageResult list(@RequestBody SysDictType dictType) {
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }

    /**
     * 查询字典类型详细
     */
    @ApiOperation("查询字典类型详细")
    @GetMapping(value = "/{dictId}")
    @SaCheckPermission("system:dict:type:info")
    public AjaxResult getInfo(@PathVariable Long dictId) {
        return AjaxResult.SUCCESS(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典类型")
    @PostMapping
    @Log(title = "新增字典类型", businessType = BusinessTypeEnum.INSERT)
    @SaCheckPermission("system:dict:type:add")
    public AjaxResult add(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return AjaxResult.FAIL("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
//        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @ApiOperation("修改字典类型")
    @PostMapping("edit")
    @Log(title = "修改字典类型", businessType = BusinessTypeEnum.INSERT)
    @SaCheckPermission("system:dict:type:edit")
    public AjaxResult edit(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return AjaxResult.FAIL("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
//        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictTypeService.updateDictType(dict));
    }

    /**
     * 获取字典数据 属性结构
     * @return
     */
    @GetMapping("getDictTreeData")
    @SaCheckPermission("sys:dict:getDictTreeData")
    @ApiOperation("获取字典数据 树形结构")
    @Deprecated
    public AjaxResult getDictTreeData(String dictType,String dictName){
        List<TreeSelect> dictTreeData = dictTypeService.getDictTreeData(dictType, dictName);
        return AjaxResult.SUCCESS(dictTreeData);



    }


    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典类型")
    @PostMapping("del")
    @Log(title = "删除字典类型", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("system:dict:type:del")
    public AjaxResult remove(@RequestBody Long[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return success();
    }

    /**
     * 刷新字典缓存
     */
    @ApiOperation("刷新字典缓存")
    @GetMapping("/refreshCache")
    @Log(title = "刷新字典缓存", businessType = BusinessTypeEnum.DELETE)
    @SaCheckPermission("system:dict:type:refreshCache")
    public AjaxResult refreshCache() {
        dictTypeService.resetDictCache();
        return AjaxResult.SUCCESS();
    }

    /**
     * 获取字典选择框列表
     */
    @ApiOperation("获取字典选择框列表")
    @GetMapping("/optionselect")
    @SaCheckPermission("system:dict:type:optionselect")
    public AjaxResult optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return AjaxResult.SUCCESS(dictTypes);
    }
}
